export default {
  computed: {
    vipDiscount() {
      // 首单立减金额信息
      return +this.orderInfo.firstDiscounts
    },
    useVipFirstDiscount() {
      // 是否使用会员首单立减
      return this.orderInfo?.discountType == 1
    },
    vipCardPrice() {
      // vip卡价格
      return this.checkVipCardV2Info.ruleId ? +this.checkVipCardV2Info.vipCard.rule.discountPrice : 0
    }
  },
  data() {
    return {
      vipDiscountVisible: false,
      openVipVisible: false,
      questionVisible: false,
      openVipInfo: {}, // 开卡信息
      // 是否使用了vip首单立减
      vipForm: {
        vipFirstDiscount: "1"
      },
      formVipFirstDiscount: "1",
      // 选中的vip卡信息
      checkVipCardV2Info: {
        ruleId: "",
        vipCardId: ""
      },
      questionList: ["1、首单立减为至尊会员权益期间每月享受首单立减优惠。", "2、仅享受在线选座金额立减，不支持抵扣会员权益费用。"]
    }
  },
  methods: {
    // 获取推荐会员卡信息
    handleGetHeightVipCardInfo(filmOrderId = this.orderId) {
      uni.$api.vipCardV2.getHeightVipCardInfo({ filmOrderId }).then((res) => {
        this.openVipInfo = res
        // 有数据展示vip卡轮播
        this.openVipVisible = uni.$check.isNoEmpty(res)
        if (this.checkVip.vipCard) {
          this.checkVip.usableCouponCount = res.usableCouponCount
          this.$store.commit("SET_CHECK_VIP", { ...this.checkVip, ...this.openVipInfo.benefit, ...this.checkVipCardV2Info })
        }
      })
    },
    handleOpenVipCheck(e) {
      // 选vip卡开卡
      if (this.checkVipCardV2Info.ruleId == e.id) {
        this.checkVipCardV2Info = {
          ruleId: "",
          vipCardId: "",
          vipCard: ""
        }
        this.vipForm.vipFirstDiscount = "0"
        this.formVipFirstDiscount = "0"
        this.preInfoReset()
      } else {
        if (!this.checkVipCardV2Info.ruleId) {
          this.preInfoReset()
        }
        this.checkVipCardV2Info = {
          ruleId: e.id,
          vipCardId: e.vipCardId,
          vipCard: e
        }
        this.vipForm.vipFirstDiscount = "1"
        this.formVipFirstDiscount = "1"
      }
      this.cancelUseFirstDiscounts()
      this.$store.commit(
        "SET_CHECK_VIP",
        this.checkVipCardV2Info.ruleId ? { ...this.openVipInfo.benefit, usableCouponCount: this.openVipInfo.usableCouponCount, ...this.checkVipCardV2Info, vipCard: e } : {}
      )
      this.createPreInfo()
    },
    // 是否使用折扣，通知服务端
    cancelUseFirstDiscounts() {
      const isUseFirstBenefit = JSON.stringify(this.formVipFirstDiscount == "1")
      uni.$api.vipCardV2.cancelUseFirstDiscounts({ orderId: this.orderId, isUseFirstBenefit, vipCardId: this.checkVipCardV2Info.vipCardId }).then(() => {
        this.getOrderInfo()
        this.getUsableCount()
        this.handleGetHeightVipCardInfo()
      })
    },
    handleVipDiscount() {
      // 改变选择发起请求
      if (this.formVipFirstDiscount != this.vipForm.vipFirstDiscount) {
        this.formVipFirstDiscount = this.vipForm.vipFirstDiscount
        this.preInfoReset()
        this.cancelUseFirstDiscounts()
      }
      this.vipDiscountVisible = false
    },
    handleQuestion() {
      this.questionVisible = true
    }
  }
}
